package com.yswee.framework.widget.clickedtab;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.yswee.framework.widget.layoutview.MRelativeLayout;

public abstract class ClickedTabBar extends MRelativeLayout<Integer> {
	
	protected View[] childviewList;
	protected IClickedTabContext listener = null;

	protected int mTabIndex;

	public int getCurrentTabIndex() {
		return mTabIndex;
	}

	public void setListener(IClickedTabContext l){
		this.listener = l;
	}
	
	public ClickedTabBar(Context context) {
		super(context);
	}
	
	public ClickedTabBar(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	@Override
	protected void onBindListener() {
		int[] childResID = getChildViewResIDs();
		if(childResID != null && childResID.length > 0) {
			childviewList = new View[childResID.length];
			for(int i = 0; i < childResID.length; i++) {
				View view = findViewById(childResID[i]);
				childviewList[i] = view;
	            if (view != null){
	            	final int index = i;
	            	view.setOnClickListener(new OnClickListener() {
	            		public void onClick(View v) {
	            			if(null != listener) {
	            				listener.onTabChanged(index);
	            			}
	            			setTabIndex(index);
	            		}
	            	});
	            }
			}
		}
	}

	@Override
	protected void onApplyData() {
		
	}

	public void setTabIndex(int index) {
		mTabIndex = index;
		if(childviewList != null) {
			for(int i = 0; i < childviewList.length; i++) {
				if(childviewList[i] != null) {
		    		if(i == index) {
		    			focusChildView(i, childviewList[i]);
		    		} else {
		    			unfocusChildView(i, childviewList[i]);
		    		}
				}
	    	}
		}
	}
	
	protected abstract int[] getChildViewResIDs();
    
	protected abstract void focusChildView(int index, View v);
    
	protected abstract void unfocusChildView(int index, View v);

}
